package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Role;
import com.atguigu.service.PermissionService;
import com.atguigu.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author:ahang
 * @create:2022-07-19 16:07
 * @Description:
 */
@Controller
@RequestMapping("/role")
public class RoleConreoller extends BaseController {

    @Reference
    RoleService roleService;


    /*@RequestMapping
    public String findAll(Model model){
        List<Role> roles = roleService.findAll();
        model.addAttribute("roles",roles);

        return "role/index";
    }*/

    @RequestMapping
    public String findPage(Model model, HttpServletRequest request){
        //获取查询条件(roleName)和分页条件(pageNum,pageSize)
        Map<String,Object> filters = getFilters(request);

        //访问业务层完成分页查询
        PageInfo<Role> page = roleService.findPage(filters);
        model.addAttribute("page",page);//分页数据；当前页数据,分页条的参数
        model.addAttribute("filters",filters);//查询条件和分页的两个参数(pageNum,pageSize)

        return "role/index";
    }


    @RequestMapping("/create")
    public String create(){
        return "role/create";
    }

    @RequestMapping("/save")
    public String insert(Role role){
        roleService.insert(role);

        return "common/successPage.html";
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Model model){
        Role role = roleService.getById(id);
        model.addAttribute("role",role);
        return "role/edit.html";
    }

    @RequestMapping("/update")
    public String update(Role role){
        roleService.update(role);
        return "common/successPage.html";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id){
        roleService.delete(id);
        return "redirect:/role"; //添加或者修改完毕之后建议使用重定向，避免表单的重复提交
    }

    @Reference
    private PermissionService permissionService;

    /**
     * 进入分配权限页面
     * @param roleId
     * @return
     */
    @RequestMapping("/assignShow/{roleId}")
    public String assignShow(@PathVariable Long roleId, Model model){
        model.addAttribute("roleId",roleId);
        List<Map<String, Object>> zNodes = permissionService.findPermissionsByRoleId(roleId);
        model.addAttribute("zNodes",zNodes);

        return "role/assignShow";
    }

    /**
     * 给角色分配权限
     * @param roleId
     * @param permissionIds
     * @return
     */
    @RequestMapping("/assignPermission")
    public String assignPermission(Long roleId,Long[] permissionIds){
        permissionService.saveRolePermissionRealtionShip(roleId,permissionIds);

        return "common/successPage";
    }
}
